<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">
        <img src="@/assets/imgs/logo.png" alt="" class="logo">
        <h2>党团信息管理系统</h2>
      </div>

      <el-form :model="data.form" ref="formRef" :rules="data.rules" class="login-form">
        <el-form-item prop="username">
          <el-input
              :prefix-icon="User"
              size="large"
              v-model="data.form.username"
              placeholder="请输入账号"
              class="custom-input"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              :prefix-icon="Lock"
              size="large"
              v-model="data.form.password"
              placeholder="请输入密码"
              show-password
              class="custom-input"
          />
        </el-form-item>
        <el-form-item>
          <el-button
              size="large"
              type="primary"
              class="login-button"
              @click="login"
          >登 录</el-button>
        </el-form-item>
      </el-form>

      <div class="register-link">
        还没有账号？<a href="/register">立即注册</a>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { reactive, ref } from "vue";
  import { User, Lock } from "@element-plus/icons-vue";
  import request from "@/utils/request";
  import {ElMessage} from "element-plus";
  import router from "@/router";
  import {getUserInfo, setUserInfo} from "@/utils/auth";

  const data = reactive({
    form: {
      username: '',
      password: ''
    },
    rules: {
      username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
      ],
    }
  })

  const formRef = ref()

  // 点击登录按钮的时候会触发这个方法
  // Login.vue 中的修改
  const login = () => {
    formRef.value.validate((valid => {
      if (valid) {
        request.post('/sysUser/login', data.form).then(res => {
          if (res.code === '200') {
            ElMessage.success("登录成功")
            setUserInfo(res.data)
            router.push('/')
          } else {
            ElMessage.error(res.msg)
          }
        })
      }
    })).catch(error => {
      console.error(error)
    })
  }



</script>


<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #c8161d 0%, #e54b4b 100%);
  position: relative;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('@/assets/imgs/pattern.png') repeat;
  opacity: 0.1;
}

.login-box {
  width: 400px;
  padding: 40px;
  border-radius: 12px;
  background-color: rgba(255, 255, 255, 0.95);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  position: relative;
}

.login-title {
  text-align: center;
  margin-bottom: 40px;
}

.login-title .logo {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
}

.login-title h2 {
  font-size: 24px;
  color: #c8161d;
  margin: 0;
  font-weight: bold;
  letter-spacing: 2px;
}

.login-form {
  margin-top: 20px;
}

.custom-input :deep(.el-input__wrapper) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border-radius: 8px;
  padding: 8px 15px;
}

.custom-input :deep(.el-input__inner) {
  height: 42px;
}

.custom-select {
  width: 100%;
}

.custom-select :deep(.el-input__wrapper) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border-radius: 8px;
}

.login-button {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  background: linear-gradient(90deg, #c8161d, #e54b4b);
  border: none;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(200, 22, 29, 0.3);
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.register-link a {
  color: #c8161d;
  text-decoration: none;
  font-weight: 500;
  margin-left: 4px;
}

.register-link a:hover {
  text-decoration: underline;
}

/* 添加输入框focus状态的样式 */
:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #c8161d !important;
}

/* 添加表单验证动画 */
.el-form-item.is-error :deep(.el-input__wrapper) {
  animation: shake 0.5s;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

/* 响应式适配 */
@media screen and (max-width: 480px) {
  .login-box {
    width: 90%;
    padding: 30px 20px;
  }

  .login-title h2 {
    font-size: 20px;
  }
}
</style>